package com.example.contestsystem.Service;

import com.example.contestsystem.Dao.JoinTeamRepository;
import com.example.contestsystem.Dao.TeacherRepository;
import com.example.contestsystem.Entity.Teacher;
import org.springframework.stereotype.Service;

@Service
public class TeacherService {
    final TeacherRepository teacherRepository;
    final JoinTeamRepository joinTeamRepository;

    public TeacherService(TeacherRepository teacherRepository, JoinTeamRepository joinTeamRepository){
        this.teacherRepository = teacherRepository;
        this.joinTeamRepository = joinTeamRepository;
    }

    public Teacher register(Teacher teacher){
        this.teacherRepository.save(teacher);
        return this.teacherRepository.findByTeacherAccount(teacher.getTeacherAccount());
    }

    public Boolean login(Teacher teacher){
        Teacher destinationTeacher = this.teacherRepository.findByTeacherAccount(teacher.getTeacherAccount());
        if(destinationTeacher != null){
            return destinationTeacher.getTeacherPassword().equals(teacher.getTeacherPassword());
        }
        return false;
    }

    public void modify(Teacher teacher){
        int destinationStuAccount = teacher.getTeacherAccount();
        if(teacher.getTeacherName()!=null){
            this.teacherRepository.modifyName(teacher.getTeacherName(), destinationStuAccount);
        }
        if(teacher.getTeacherPassword()!=null){
            this.teacherRepository.modifyPassword(teacher.getTeacherPassword(), destinationStuAccount);
        }
    }

    public Teacher findByTeacherAccount(int teacherAccount){
        return this.teacherRepository.findByTeacherAccount(teacherAccount);
    }

    public boolean verifyTeacher(int stuAccount){
        return this.teacherRepository.findByTeacherAccount(stuAccount) != null;
    }

}
